/*
 * @Author: ykp 501436989@qq.com
 * @Date: 2025-05-03 09:59:55
 * @LastEditors: ykp 501436989@qq.com
 * @LastEditTime: 2025-08-14 15:53:39
 * @FilePath: \vue-ts\src\views\todolist\gojson.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import { urgencyList as urgencyListJson, taskCategory as taskCategoryJson, taskStatus as taskStatusJson, } from './gojson'
export const querylist = function (name: string) {
  return new Promise((resolve, reject) => {
    // 打开数据库
    const request = indexedDB.open('myDatabase', 1);
    let arrs: any[] = []
    // 数据库打开成功时触发
    request.onsuccess = function (event: any) {
      const db = event.target.result;
      // 开始一个只读事务
      const transaction = db.transaction([name], 'readonly');
      const objectStore = transaction.objectStore(name);
      // 打开游标，遍历对象存储空间中的所有数据
      const cursorRequest = objectStore.openCursor();
      cursorRequest.onsuccess = function (event: any) {
        const cursor = event.target.result;
        if (cursor) {
          arrs.push(structuredClone(cursor.value))
          cursor.continue();
        } else {
          resolve(structuredClone(arrs))
          db.close()
        }
      };
      cursorRequest.onerror = function (event: any) {
        reject(new Error('数据库打开失败'));
        db.close()
      };
      // 事务完成时触发
      transaction.oncomplete = function (event: any) {
        db.close();
      };
      // 事务出错时触发
      transaction.onerror = function (event: any) {
        // console.log('事务出错:', event.target.error);
        reject(new Error('事务出错'));
      };
    };
    // 数据库打开失败时触发
    request.onerror = function (event: any) {
      // console.log('数据库打开失败');
      reject(new Error('数据库打开失败'));
    };
  })
}


/**
 * @description: 任务紧急程度 任务紧急程度数据 默认是indexDB 兜底是gojson 数据
 * @return {*} 
 */
import type { ListType } from "@/type/todolist/card"; //引入ts类型定义
export const urgencyList = async function () {
  try {

  let arrs:ListType[] = await querylist('urgencyList') as ListType[]
    if (Object.prototype.toString.call(arrs) == '[object Array]') {
      return arrs 
    } else {
      return urgencyListJson
    }

  } catch (errs) {
    console.log(errs)
    return urgencyListJson
  }
}
/**
 * @description: 任务类别数据 默认是indexDB 中  兜底是gojson 数据
 * @return {*}
 */
export const taskCategory = async function () {
  try {

    let arrs = await querylist('taskCategory') as ListType[]
    if (Object.prototype.toString.call(arrs) == '[object Array]') {
      return arrs
    } else {
      return taskCategoryJson
    }

  } catch (errs) {
    console.log(errs)
    return taskCategoryJson
  }
}
/**
 * @description: 任务状态默认是indexDB 中  兜底是gojson 数据 
 * @return {*}
 */
export const taskStatus = async function () {
  try {

    let arrs:ListType[] = await querylist('taskStatus') as ListType[]
    if (Object.prototype.toString.call(arrs) == '[object Array]') {
      return arrs
    } else {
      return taskStatusJson
    }

  } catch (errs) {
    console.log(errs)
    return taskStatusJson
  }
}